#!/bin/sh
## ./doc_guide/check.sh g++ .
## or
## ./doc_guide/check.sh clang++ .

if [ ! -n "$1" ]; then
    echo "missing param, please input compiler name, clang++ or g++"
    exit 1
fi

if [ "$1" = "g++" ]; then
    concepts=-fconcepts
fi

if [ ! -n "$2" ]; then
    echo "missing param, please input Mix-C project root path"
    exit 1
fi

cxx=$1
path_usage=$2/doc_guide/
checkout_file=check.txt

simd="-mavx -mavx2"
include="-I$2"
opt="-g"
warn="-Wall -Wextra"
cflags=" $include $warn $concepts -std=c++2a -Wno-empty-body -pthread $opt "
part_extern="extern.api_$cxx"

color_error="\033[31m"
color_default="\033[32m"

xhint(){
    echo $color_default$1
}

xerror(){
    echo $color_error$1
}

xhint "check path:$path_usage"

if [ ! -f $2/../$part_extern.o ]; then
    xhint "not exist $part_extern.o"
    xhint "$cxx compiling the $part_extern.o"
    xhint "$cxx $cflags $2/$part_extern.cpp -c"
    $cxx $cflags $2/$part_extern.cpp -c 2> /dev/null
    result=$?

    if [ $result != 0 ]; then
        xerror $high_light"error! compiling the $part_extern.o failed"
        exit $result
    fi
fi

rm $checkout_file

find $path_usage -type f -name "*.cpp" | while read line
do
    params="$cflags $2/../$part_extern.o $line -o $2/../main.exe"
    $cxx $params 2>> $checkout_file

    if [ $? = 0 ]; then
        xhint  "[PASS] $line"
    else
        xerror "[FAIL] $line"
    fi
done
